package xyz.labmem.base.sys.config

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import springfox.documentation.builders.*
import springfox.documentation.service.ApiInfo
import springfox.documentation.service.Contact
import springfox.documentation.service.RequestParameter
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spring.web.plugins.Docket
import xyz.labmem.base.sys.consts.PathPrefixConst


/**
 * @Author lty
 * @Date 2021/12/14 16:17
 */
@Configuration
@EnableKnife4j
class SwaggerConfig(
    private var openApiExtensionResolver: OpenApiExtensionResolver
) {

    @Value(value = "\${sa-token.token-name}")
    private val tokenName: String? = null

    @Value(value = "\${sa-token.tokenPrefix}")
    private val tokenPrefix: String? = null

    @Bean
    fun allApi(): Docket =
        Docket(DocumentationType.OAS_30)
            .apiInfo(apiInfo())
            .pathMapping("/")
            .groupName("全部接口")
            .select()
        //    .apis(RequestHandlerSelectors.basePackage("xyz.labmem.main.modular.app.gmjj.controller"))
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation::class.java))
            .paths(PathSelectors.any())
            .build()
            .extensions(openApiExtensionResolver.buildSettingExtensions())
            .globalRequestParameters(getParameters())

    @Bean
    fun authApi(): Docket =
        Docket(DocumentationType.OAS_30)
            .apiInfo(apiInfo())
            .pathMapping("/")
            .groupName("权限接口")
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.ant("${PathPrefixConst.AUTH_PATH_PREFIX}**"))
            .build()
            .extensions(openApiExtensionResolver.buildSettingExtensions())
            .globalRequestParameters(getParameters())


    @Bean
    fun sysApi(): Docket =
        Docket(DocumentationType.OAS_30)
            .apiInfo(apiInfo())
            .pathMapping("/")
            .groupName("系统接口")
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.ant("${PathPrefixConst.SYS_PATH_PREFIX}**"))
            .build()
            .extensions(openApiExtensionResolver.buildSettingExtensions())
            .globalRequestParameters(getParameters())


    @Bean
    fun appApi(): Docket =
        Docket(DocumentationType.OAS_30)
            .apiInfo(apiInfo())
            .pathMapping("/")
            .groupName("应用接口")
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.ant("${PathPrefixConst.APP_PATH_PREFIX}**"))
            .build()
            .extensions(openApiExtensionResolver.buildSettingExtensions())
            .globalRequestParameters(getParameters())


    @Bean
    fun commonApi(): Docket =
        Docket(DocumentationType.OAS_30)
            .apiInfo(apiInfo())
            .pathMapping("/")
            .groupName("公共接口")
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.ant("${PathPrefixConst.COMMON_PATH_PREFIX}**"))
            .build()
            .extensions(openApiExtensionResolver.buildSettingExtensions())
            .globalRequestParameters(getParameters())


    private fun apiInfo(): ApiInfo =
        ApiInfoBuilder()
            .title("Labmem SpringBoot 接口文档")
            .description("Labmem Project 后台开发框架")
            .contact(Contact("labmem", "https://labmem.xyz/", "labmem@labmem.xyz"))
            .version("1.1.0")
            .build()


    private fun getParameters(): List<RequestParameter> =
        arrayListOf<RequestParameter>(
            RequestParameterBuilder()
                .name(tokenName)
                .description("token令牌 前缀:$tokenPrefix")
                .required(false)
                .`in`("header")
                .build()
        )


}